package com.qeegoo.b2bautozityp.moudle.inquiry.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.constant.Constants;
import com.qeegoo.b2bautozityp.databinding.FragmentInquiryListBinding;
import com.qeegoo.b2bautozityp.injector.component.DaggerInquiryListFragmentComponent;
import com.qeegoo.b2bautozityp.injector.module.InquiryListFragmentModule;
import com.qeegoo.b2bautozityp.moudle.base.LazyLoadFragment;
import com.qeegoo.b2bautozityp.moudle.inquiry.adapter.InquiryFragmentAdapter;
import com.qeegoo.b2bautozityp.moudle.inquiry.model.InquiryListBean;
import com.qeegoo.b2bautozityp.moudle.inquiry.viewmodel.InquiryListViewModel;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by lining on 2017/9/29.
 */

public class InquiryListFragment extends LazyLoadFragment<FragmentInquiryListBinding> {

    @Inject
    LinearLayoutManager mLayuoutManager;

    @Inject
    InquiryListViewModel viewModel;

    private int pageNo = 1;
    private int totalPages;
    private InquiryListBean resultData;

    public static InquiryListFragment newInstance(int orderType) {

        Bundle args = new Bundle();
        args.putInt("inquiryType", orderType);
        InquiryListFragment fragment = new InquiryListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.e("OrderFragment", getArguments().get(Constants.Fragment_OrderType) + "");
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_inquiry_list;
    }

    @Override
    protected void initInjector() {
        DaggerInquiryListFragmentComponent.builder().appComponent(getAppComponent()).inquiryListFragmentModule(new InquiryListFragmentModule(this)).build().inject(this);
    }

    @Override
    protected void initViews() {

        mBinding.orderRv.setLayoutManager(mLayuoutManager);
        mBinding.setViewModel(viewModel);
        mBinding.swipeLayout.setColorSchemeResources(R.color.colorPrimary);
        mBinding.swipeLayout.setOnRefreshListener(() -> {
            viewModel.getDatas("1", getCurrentOrderType());
        });
        mBinding.orderRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    int lastVisiblePosition = linearLayoutManager.findLastVisibleItemPosition();
                    if (lastVisiblePosition >= linearLayoutManager.getItemCount() - 1) {
                        if (pageNo < totalPages) {
                            viewModel.getDatas(++pageNo + "", getCurrentOrderType());
                        }
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

            }

        });

        InquiryFragmentAdapter orderAdapter = new InquiryFragmentAdapter();
        mBinding.orderRv.setAdapter(orderAdapter);
        viewModel.setAdapterInterface(data -> {
            resultData = data;
            List<InquiryListBean.ListBean> datas = data.list;
            if (mBinding.swipeLayout != null) {
                mBinding.swipeLayout.setRefreshing(false);

            }
            if (data.list == null || data.list.size() == 0) {
                viewModel.emptyVisible.set(View.VISIBLE);
            } else {
                viewModel.emptyVisible.set(View.GONE);
            }
            if (data.curPageNo == 1) {
                orderAdapter.addAllDatas(datas);
            }else {
                orderAdapter.addDatas(datas);
            }
            totalPages = data.totalPages;
            pageNo = data.curPageNo;
        });
        orderAdapter.setOnItemClickListener(((view, position) -> {
//            if (resultData!=null){
//                InquiryListBean.ListBean listBean = resultData.list.get(position);
//                ToastUtils.showToast(listBean.carModelName);
//                Bundle params = new Bundle();
//                params.putString(Constants.Param_code,listBean.id);
//                NavigateUtils.startActivity(OrderDetailActivity.class,params);
//            }


        }));
    }

    @Override
    public void requestData() {

        mBinding.swipeLayout.post(new Runnable() {
            @Override
            public void run() {
                mBinding.swipeLayout.setRefreshing(true);
            }
        });
        viewModel.getDatas("1", getCurrentOrderType());
    }

    private String getCurrentOrderType() {
        if (getArguments() != null) {
            return getArguments().getInt("inquiryType") + "";
        }
        return "0";
    }

}
